﻿using Common;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using NursingFormSystem.NurFormsService;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NursingFormSystem.KnowingAgreeBook
{
    public partial class AgreeBookNursing : Form
    {
        #region 变量声明
        string[] patientinfo;
        string esSwitch = Base.ESSwitch;//获取是否使用电签标识
        private gregn6Lib.GridppReport gpr_Feport = new gregn6Lib.GridppReport();//引用对象
        NursingFormRecord record = new NursingFormRecord();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        string[] Patient;//选前选中患者信息，数组，传入调用的form中
        string Id = "";//还未用到，留着变量做多张表单用
        
        public static Tb_User LoginUser;
        public Tb_User InitLoginUser(string[] UserParam)
        {
            if (LoginUser == null)
            {
                LoginUser = new Tb_User();
                LoginUser.UserId = UserParam[0];
                LoginUser.UserName = UserParam[1];
                LoginUser.DepartmentCode = UserParam[2];
                LoginUser.DepartmentName = UserParam[3];
                LoginUser.JobTitle = UserParam[4];
                LoginUser.PositionTitle = UserParam[5];
            }
            return LoginUser;
        }
        #endregion

        #region 窗体构造函数
        public AgreeBookNursing(string[] UserParm, string[] PatientInfo, string Id)
        {
            InitializeComponent();
            this.Id = Id;
            InitLoginUser(UserParm);
            patientinfo = PatientInfo;
            Patient = PatientInfo;
            DateE_RiQi.Text = DateTime.Now.ToShortDateString();
        }
        #endregion

        #region 查询
        private void btn_Query_Click(object sender, EventArgs e)
        {
            str(Id);
        }

        //显示
        public void str(string Id)
        {
            var ret = formService.GetNursingFormRecord(Patient[0], Convert.ToInt32(Patient[1]), (int)FormType.Knowing);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                NursingFormRecord record = list[0];//只有一条
                Knowing ps = XmlUtil.XMLToObject<Knowing>(record.NursingFormData, Encoding.UTF8);
                //约束工具的应用
                txtYY_QiTa.Text = ps.YueShuGongJu.YY_QiTa;//原因-其他
                txtYF_QiTa.Text = ps.YueShuGongJu.YF_QiTa;//预防-其他
                txtBFZ_QiTa.Text = ps.YueShuGongJu.BFZ_QiTa;//并发症-其他
                var YuanYin = ps.YueShuGongJu.YuanYin.Split('#'); //原因
                if (YuanYin[0] == "1")
                    cheYuanYin1.Checked = true;
                else
                    cheYuanYin1.Checked = false;
                if (YuanYin[1] == "1")
                    cheYuanYin2.Checked = true;
                else
                    cheYuanYin2.Checked = false;
                if (YuanYin[2] == "1")
                    cheYuanYin3.Checked = true;
                else
                    cheYuanYin3.Checked = false;
                if (YuanYin[3] == "1")
                    cheYuanYin4.Checked = true;
                else
                    cheYuanYin4.Checked = false;
                var YuFang = ps.YueShuGongJu.YuFang.Split('#'); //预防
                if (YuFang[0] == "1")
                    cheYuFang1.Checked = true;
                else
                    cheYuFang1.Checked = false;
                if (YuFang[1] == "1")
                    cheYuFang2.Checked = true;
                else
                    cheYuFang2.Checked = false;
                if (YuFang[2] == "1")
                    cheYuFang3.Checked = true;
                else
                    cheYuFang3.Checked = false;
                if (YuFang[3] == "1")
                    cheYuFang4.Checked = true;
                else
                    cheYuFang4.Checked = false;
                if (YuFang[4] == "1")
                    cheYuFang5.Checked = true;
                else
                    cheYuFang5.Checked = false;
                var BingFaZheng = ps.YueShuGongJu.BingFaZheng.Split('#'); //并发症
                if (BingFaZheng[0] == "1")
                    cheBingFaZheng1.Checked = true;
                else
                    cheBingFaZheng1.Checked = false;
                if (BingFaZheng[1] == "1")
                    cheBingFaZheng2.Checked = true;
                else
                    cheBingFaZheng2.Checked = false;
                if (BingFaZheng[2] == "1")
                    cheBingFaZheng3.Checked = true;
                else
                    cheBingFaZheng3.Checked = false;
                if (BingFaZheng[3] == "1")
                    cheBingFaZheng4.Checked = true;
                else
                    cheBingFaZheng4.Checked = false;

                //24小时看护（患方）
                txtYSZA_QiTa.Text = ps.ErShiSiXiaoShiKanHu.YSZA_QiTa;//意识障碍-其他
                txtJSZA_QiTa.Text = ps.ErShiSiXiaoShiKanHu.JSZA_QiTa;//精神障碍-其他
                txtNL_QiTa.Text = ps.ErShiSiXiaoShiKanHu.NL_QiTa;//年龄-其他
                txtYF_QiTa1.Text = ps.ErShiSiXiaoShiKanHu.YF_QiTa;//预防-其他
                var YiShiZhangAi = ps.ErShiSiXiaoShiKanHu.YiShiZhangAi.Split('#'); //意识障碍
                if (YiShiZhangAi[0] == "1")
                    cheYiShiZhangAi1.Checked = true;
                else
                    cheYiShiZhangAi1.Checked = false;
                if (YiShiZhangAi[1] == "1")
                    cheYiShiZhangAi2.Checked = true;
                else
                    cheYiShiZhangAi2.Checked = false;
                if (YiShiZhangAi[2] == "1")
                    cheYiShiZhangAi3.Checked = true;
                else
                    cheYiShiZhangAi3.Checked = false;
                if (YiShiZhangAi[3] == "1")
                    cheYiShiZhangAi4.Checked = true;
                else
                    cheYiShiZhangAi4.Checked = false;
                var JingShenZhangAi = ps.ErShiSiXiaoShiKanHu.JingShenZhangAi.Split('#'); //精神障碍
                if (JingShenZhangAi[0] == "1")
                    cheJingShenZhangAi1.Checked = true;
                else
                    cheJingShenZhangAi1.Checked = false;
                if (JingShenZhangAi[1] == "1")
                    cheJingShenZhangAi2.Checked = true;
                else
                    cheJingShenZhangAi2.Checked = false;
                if (JingShenZhangAi[2] == "1")
                    cheJingShenZhangAi3.Checked = true;
                else
                    cheJingShenZhangAi3.Checked = false;
                var NianLin = ps.ErShiSiXiaoShiKanHu.NianLin.Split('#'); ////年龄
                if (NianLin[0] == "1")
                    cheNianLin1.Checked = true;
                else
                    cheNianLin1.Checked = false;
                if (NianLin[1] == "1")
                    cheNianLin2.Checked = true;
                else
                    cheNianLin2.Checked = false;
                var YuFang1 = ps.ErShiSiXiaoShiKanHu.YuFang.Split('#'); //预防
                if (YuFang1[0] == "1")
                    cheYuFang11.Checked = true;
                else
                    cheYuFang11.Checked = false;
                if (YuFang1[1] == "1")
                    cheYuFang22.Checked = true;
                else
                    cheYuFang22.Checked = false;
                if (YuFang1[2] == "1")
                    cheYuFang33.Checked = true;
                else
                    cheYuFang33.Checked = false;
                if (YuFang1[3] == "1")
                    cheYuFang44.Checked = true;
                else
                    cheYuFang44.Checked = false;
                if (YuFang1[4] == "1")
                    cheYuFang55.Checked = true;
                else
                    cheYuFang55.Checked = false;
                if (YuFang1[5] == "1")
                    cheYuFang66.Checked = true;
                else
                    cheYuFang66.Checked = false;

                //尾部
                txtJiaShuYiJian.Text = ps.JiaShuYiJian;//家属意见
                txtQianMing.Text = ps.QianMing;//签名
                txtYuHuanZheGuanXi.Text = ps.YuHuanZheGuanXi;//与患者关系
                txtHuShiQianMing.Text = ps.HuShiQianMing;//护士签名
                DateE_RiQi.Text = ps.RiQi;//日期
                if (ps.RiQi.Contains("0001/01/01") || ps.RiQi.Contains("1900"))
                {
                    DateE_RiQi.Text = "";
                }
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
            else if (ret.Code == 2)
            {
                //约束工具的应用
                txtYY_QiTa.Text = "";//原因-其他
                txtYF_QiTa.Text = "";//预防-其他
                txtBFZ_QiTa.Text = "";//并发症-其他
                //原因
                cheYuanYin1.Checked = false;
                cheYuanYin2.Checked = false;
                cheYuanYin3.Checked = false;
                cheYuanYin4.Checked = false;
                //预防
                cheYuFang1.Checked = false;
                cheYuFang2.Checked = false;
                cheYuFang3.Checked = false;
                cheYuFang4.Checked = false;
                cheYuFang5.Checked = false;
                //并发症
                cheBingFaZheng1.Checked = false;
                cheBingFaZheng2.Checked = false;
                cheBingFaZheng3.Checked = false;
                cheBingFaZheng4.Checked = false;

                //24小时看护（患方）
                txtYSZA_QiTa.Text = "";//意识障碍-其他
                txtJSZA_QiTa.Text = "";//精神障碍-其他
                txtNL_QiTa.Text = "";//年龄-其他
                txtYF_QiTa1.Text = "";//预防-其他
                //意识障碍
                cheYiShiZhangAi1.Checked = false;
                cheYiShiZhangAi2.Checked = false;
                cheYiShiZhangAi3.Checked = false;
                cheYiShiZhangAi4.Checked = false;
                //精神障碍
                cheJingShenZhangAi1.Checked = false;
                cheJingShenZhangAi2.Checked = false;
                cheJingShenZhangAi3.Checked = false;
                //年龄
                cheNianLin1.Checked = false;
                cheNianLin2.Checked = false;
                //预防
                cheYuFang11.Checked = false;
                cheYuFang22.Checked = false;
                cheYuFang33.Checked = false;
                cheYuFang44.Checked = false;
                cheYuFang55.Checked = false;
                cheYuFang66.Checked = false;

                //尾部
                txtJiaShuYiJian.Text = "";//家属意见
                txtQianMing.Text = "";//签名
                txtYuHuanZheGuanXi.Text = "";//与患者关系
                txtHuShiQianMing.Text = "";//护士签名
                DateE_RiQi.DateTime = DateTime.Now;//日期
            }
        }
        #endregion

        #region 保存
        private void btn_Preservation_Click(object sender, EventArgs e)
        {
            Knowing bc = new Knowing();
            bc.FormId = (int)FormType.Knowing;
            bc.FormName = EnumHelper.GetDescription(FormType.Knowing);
            //约束工具的应用
            bc.YueShuGongJu.YY_QiTa = txtYY_QiTa.Text;//原因-其他
            bc.YueShuGongJu.YF_QiTa = txtYF_QiTa.Text;//预防-其他
            bc.YueShuGongJu.BFZ_QiTa = txtBFZ_QiTa.Text;//并发症-其他
            bc.YueShuGongJu.YuanYin = (cheYuanYin1.Checked == true ? "1" : "0") + "#" + (cheYuanYin2.Checked == true ? "1" : "0") + "#" + (cheYuanYin3.Checked == true ? "1" : "0") + "#" + (cheYuanYin4.Checked == true ? "1" : "0");//原因
            bc.YueShuGongJu.YuFang = (cheYuFang1.Checked == true ? "1" : "0") + "#" + (cheYuFang2.Checked == true ? "1" : "0") + "#" + (cheYuFang3.Checked == true ? "1" : "0") + "#" + (cheYuFang4.Checked == true ? "1" : "0") + "#" + (cheYuFang5.Checked == true ? "1" : "0");//预防
            bc.YueShuGongJu.BingFaZheng = (cheBingFaZheng1.Checked == true ? "1" : "0") + "#" + (cheBingFaZheng2.Checked == true ? "1" : "0") + "#" + (cheBingFaZheng3.Checked == true ? "1" : "0") + "#" + (cheBingFaZheng4.Checked == true ? "1" : "0");//并发症

            //24小时看护（患方）
            bc.ErShiSiXiaoShiKanHu.YSZA_QiTa = txtYSZA_QiTa.Text;//意识障碍-其他
            bc.ErShiSiXiaoShiKanHu.JSZA_QiTa = txtJSZA_QiTa.Text;//精神障碍-其他
            bc.ErShiSiXiaoShiKanHu.NL_QiTa = txtNL_QiTa.Text;//年龄-其他
            bc.ErShiSiXiaoShiKanHu.YF_QiTa = txtYF_QiTa1.Text;//预防-其他
            bc.ErShiSiXiaoShiKanHu.YiShiZhangAi = (cheYiShiZhangAi1.Checked == true ? "1" : "0") + "#" + (cheYiShiZhangAi2.Checked == true ? "1" : "0") + "#" + (cheYiShiZhangAi3.Checked == true ? "1" : "0") + "#" + (cheYiShiZhangAi4.Checked == true ? "1" : "0");//意识障碍
            bc.ErShiSiXiaoShiKanHu.JingShenZhangAi = (cheJingShenZhangAi1.Checked == true ? "1" : "0") + "#" + (cheJingShenZhangAi2.Checked == true ? "1" : "0") + "#" + (cheJingShenZhangAi3.Checked == true ? "1" : "0");//精神障碍
            bc.ErShiSiXiaoShiKanHu.NianLin = (cheNianLin1.Checked == true ? "1" : "0") + "#" + (cheNianLin2.Checked == true ? "1" : "0");//年龄
            bc.ErShiSiXiaoShiKanHu.YuFang = (cheYuFang11.Checked == true ? "1" : "0") + "#" + (cheYuFang22.Checked == true ? "1" : "0") + "#" + (cheYuFang33.Checked == true ? "1" : "0") + "#" + (cheYuFang44.Checked == true ? "1" : "0") + "#" + (cheYuFang55.Checked == true ? "1" : "0") + "#" + (cheYuFang66.Checked == true ? "1" : "0");//预防

            //尾部
            bc.JiaShuYiJian = txtJiaShuYiJian.Text;//家属意见
            bc.QianMing = txtQianMing.Text;//签名
            bc.YuHuanZheGuanXi = txtYuHuanZheGuanXi.Text;//与患者关系
            bc.HuShiQianMing = txtHuShiQianMing.Text;//护士签名
            if (string.IsNullOrEmpty(DateE_RiQi.Text))
            {
                MessageBox.Show("请选择日期");
                return;
            }
            bc.RiQi = DateE_RiQi.DateTime.ToString();//日期
            record.NursingFormData = XmlUtil.ObjectToXML(bc, Encoding.UTF8);
            record.FormId = bc.FormId;//表单ID
            record.FormName = bc.FormName;//表单NAME
            record.PatientId = Patient[0];//住院号
            record.VisitId = Convert.ToInt32(Patient[1]);//住院次数
            record.PatientName = Patient[2];//住院名称
            record.BedNo = Patient[5];//床号
            record.DepartmentId = Patient[8];//病区号
            record.DepartmentName = Patient[9];//病区名称
            record.AreaId = Patient[10];//所在地编号
            record.AreaName = Patient[11];//所在地名称
            record.AssessmentDate = !string.IsNullOrEmpty(DateE_RiQi.Text) ? (DateE_RiQi.DateTime) : DateTime.Now;
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            var ret2 = formService.GetNursingFormRecord(patientinfo[0], Convert.ToInt32(patientinfo[1]), (int)FormType.Knowing);
            if (ret.Code == 1)
            {
                //MessageBox.Show(ret.M);
                //保存字段拼接成xml格式
                string SignXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><yueshugongjuyuanyin name=\"约束工具原因\">" + (cheYuanYin1.Checked == true ? "1" : "0") + "#" + (cheYuanYin2.Checked == true ? "1" : "0") + "#" + (cheYuanYin3.Checked == true ? "1" : "0") + "#" + (cheYuanYin4.Checked == true ? "1" : "0") + "#" + "</yueshugongjuyuanyin><yuanyinqita name=\"原因其他\">" + txtYY_QiTa.Text + "</yuanyinqita>"
                                 + "<yueshugongjuyufang name=\"约束工具预防\">" + (cheYuFang1.Checked == true ? "1" : "0") + "#" + (cheYuFang2.Checked == true ? "1" : "0") + "#" + (cheYuFang3.Checked == true ? "1" : "0") + "#" + (cheYuFang4.Checked == true ? "1" : "0") + "#" + (cheYuFang5.Checked == true ? "1" : "0") + "#" + "</yueshugongjuyufang><yufangqita name=\"预防其他\">" + txtYF_QiTa.Text + "</yufangqita>"
                                 + "<yueshugongjubingfazheng name=\"约束工具并发症\">" + (cheBingFaZheng1.Checked == true ? "1" : "0") + "#" + (cheBingFaZheng2.Checked == true ? "1" : "0") + "#" + (cheBingFaZheng3.Checked == true ? "1" : "0") + "#" + (cheBingFaZheng4.Checked == true ? "1" : "0") + "#" + "</yueshugongjubingfazheng><yuanyinqita name=\"并发症其他\">" + txtBFZ_QiTa.Text + "</yuanyinqita>"
                                 + "<24xiaokanhuyuanyinyishizhangai name=\"24小时看护原因意识障碍\">" + (cheYiShiZhangAi1.Checked == true ? "1" : "0") + "#" + (cheYiShiZhangAi2.Checked == true ? "1" : "0") + "#" + (cheYiShiZhangAi3.Checked == true ? "1" : "0") + "#" + (cheYiShiZhangAi4.Checked == true ? "1" : "0") + "#" + "</24xiaokanhuyuanyinyishizhangai><yishizhangaiqita name=\"意识障碍其他\">" + txtYSZA_QiTa.Text + "</yishizhangaiqita>"
                                 + "<24xiaokanhuyuanyinjingshenzhangai name=\"24小时看护原因精神障碍\">" + (cheJingShenZhangAi1.Checked == true ? "1" : "0") + "#" + (cheJingShenZhangAi2.Checked == true ? "1" : "0") + "#" + (cheJingShenZhangAi3.Checked == true ? "1" : "0") + "#" + (cheNianLin1.Checked == true ? "1" : "0") + "#" + (cheNianLin2.Checked == true ? "1" : "0") + "#" + "</24xiaokanhuyuanyinjingshenzhangai><jingshenzhangaiqita name=\"精神障碍其他\">" + txtJSZA_QiTa.Text + "</jingshenzhangaiqita><jingshenzhangaiqita2 name=\"精神障碍其他2\">" + txtNL_QiTa.Text + "</jingshenzhangaiqita2>"
                                 + "<24xiaokanhuyuanyinyufang name=\"24小时看护原因预防\">" + (cheYuFang11.Checked == true ? "1" : "0") + "#" + (cheYuFang22.Checked == true ? "1" : "0") + "#" + (cheYuFang33.Checked == true ? "1" : "0") + "#" + (cheYuFang44.Checked == true ? "1" : "0") + "#" + (cheYuFang55.Checked == true ? "1" : "0") + "#" + (cheYuFang66.Checked == true ? "1" : "0") + "#" + "</24xiaokanhuyuanyinyufang><yufangiqita name=\"预防其他\">" + txtYF_QiTa1.Text + "</yufangiqita>"
                                 + "<jiashuyijian name=\"家属意见\">" + txtJiaShuYiJian.Text + "</jiashuyijian><qianming name=\"签名\">" + txtQianMing.Text + "</qianming><yuhuanzheguanxi name=\"与患者关系\">" + txtYuHuanZheGuanXi.Text + "</yuhuanzheguanxi><hushiqianming name=\"护士签名\">" + txtHuShiQianMing.Text + "</hushiqianming><riqi name=\"日期\">" + DateE_RiQi.Text + "</riqi>";
                //TODO:电签接口提交
                //string extraInfo = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><root><sign_sys>业务系统名称</sign_sys><title>单据名称</title><patient_id>病人ID</patient_id><patient_name>病人姓名</patient_name><emr_id>电子病历ID</emr_id><emr_catalog>电子病历类型</emr_catalog><his_id>HIS ID</his_id><event_no>住院号或门诊号</event_no><visit_number>住院次数，0次为门诊病人</visit_number><dept_name>科室名称</dept_name><prescribe_number>医嘱号或门诊号</prescribe_number><prescribe_sub_number>医嘱子序号或门诊子序号</prescribe_sub_number><prescribe_operation>医嘱操作（发送、校对、停止、作废）或门诊处方操作（发送）</prescribe_operation><radiotherapy_id>放疗号</radiotherapy_id><process_name>CT定位申请单审核</process_name><process_time>CT定位申请单审核时间</process_time><extend_value_1>备用扩展1</extend_value_1><extend_value_2>备用扩展2</extend_value_2><extend_value_3>备用扩展3</extend_value_3><extend_value_4>备用扩展4</extend_value_4><extend_value_5>备用扩展5</extend_value_5><extend_value_6>备用扩展6</extend_value_6><extend_value_7>备用扩展7</extend_value_7><extend_value_8>备用扩展8</extend_value_8><extend_value_9>备用扩展9</extend_value_9></root>";
                //ElectronicSignature es = new ElectronicSignature(LoginUser);
                //es.GetElectronicSignatureImage(es, SignXml, extraInfo, formID, formName, patientinfo[0], int.Parse(patientinfo[1]));
                MessageBox.Show(ret.M);
            }
            else
            {
                MessageBox.Show(ret.M);
            }
                 
        }
        #endregion

        #region 打印
        private void btn_Printing_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region 整个大框
        private void AgreeBookNursing_Load(object sender, EventArgs e)
        {
            str(Id);
        }
        #endregion
    }
}
